package org.butterfly.watchdog.task;

import java.util.concurrent.ConcurrentHashMap;

/**
 * task factory
 *
 * @author drake
 * @since 2022/3/30
 */
public class TaskFactory {
    private final ConcurrentHashMap<String, Task> taskMap = new ConcurrentHashMap<>();

    private static final TaskFactory DEFAULT_FACTORY = new TaskFactory();

    static {
        DEFAULT_FACTORY.addTask(new DefaultTask());
    }

    public void doTask() {
        for (Task task : taskMap.values()) {
            task.watch();
        }
    }

    public void addTask(Task task) {
        taskMap.put(task.getClass().getName(), task);
    }

    public boolean deleteTask(Task task) {
        return taskMap.remove(task.getClass().getName(), task);
    }

    public static TaskFactory getDefaultFactory() {
        return DEFAULT_FACTORY;
    }
}
